/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * POIDisplay.java
 *
 * Created on Jul 24, 2008, 2:08:31 PM
 */

package spring;

import javax.swing.*;
import javax.swing.event.*;

/**
 *
 * @author spring
 */
public class POIBuilder extends javax.swing.JDialog  {

    /** Creates new form POIDisplay */
    public POIBuilder(spring.PoiEditor parent, boolean modal) {
        super(parent, modal);
        initComponents();
        PParent = parent;
        Terms.setModel(dtl);
        Docs.setModel(ddl);
        this.getContentPane().setBackground(bgcolor);
        XLoc.setText(new Double(X).toString());
        YLoc.setText(new Double(Y).toString());
      
   }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        Name = new javax.swing.JTextField();
        NameLab = new javax.swing.JLabel();
        Color = new javax.swing.JButton();
        TermSP = new javax.swing.JScrollPane();
        Terms = new javax.swing.JList();
        XLoc = new javax.swing.JTextField();
        YLoc = new javax.swing.JTextField();
        Ylab = new javax.swing.JLabel();
        XLab = new javax.swing.JLabel();
        LocLab = new javax.swing.JLabel();
        AddField = new javax.swing.JButton();
        SaveMod = new javax.swing.JButton();
        RemoveField = new javax.swing.JButton();
        TermName = new javax.swing.JTextField();
        TermWeight = new javax.swing.JTextField();
        Desc = new javax.swing.JLabel();
        SavePOI = new javax.swing.JButton();
        NextPOI = new javax.swing.JButton();
        DocsLab = new javax.swing.JLabel();
        TermsLab = new javax.swing.JLabel();
        DocSP = new javax.swing.JScrollPane();
        Docs = new javax.swing.JList();
        ClusterLab = new javax.swing.JLabel();
        POILab = new javax.swing.JLabel();
        AbortBut = new javax.swing.JButton();

        getContentPane().setLayout(new java.awt.GridBagLayout());

        Name.setColumns(10);
        Name.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                NameActionPerformed(evt);
            }
        });
        Name.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                NameFocusLost(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        getContentPane().add(Name, gridBagConstraints);

        NameLab.setText("Name");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        getContentPane().add(NameLab, gridBagConstraints);

        Color.setText("Set Color");
        Color.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ColorActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        getContentPane().add(Color, gridBagConstraints);

        Terms.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        Terms.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        Terms.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                TermsMouseClicked(evt);
            }
        });
        TermSP.setViewportView(Terms);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        getContentPane().add(TermSP, gridBagConstraints);

        XLoc.setColumns(5);
        XLoc.setHorizontalAlignment(javax.swing.JTextField.LEFT);
        XLoc.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                XLocActionPerformed(evt);
            }
        });
        XLoc.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                XLocFocusLost(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 8;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        getContentPane().add(XLoc, gridBagConstraints);

        YLoc.setColumns(5);
        YLoc.setHorizontalAlignment(javax.swing.JTextField.LEFT);
        YLoc.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                YLocActionPerformed(evt);
            }
        });
        YLoc.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                YLocFocusLost(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 9;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        getContentPane().add(YLoc, gridBagConstraints);

        Ylab.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        Ylab.setText("Y");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 9;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        getContentPane().add(Ylab, gridBagConstraints);

        XLab.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        XLab.setText("X");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 8;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        getContentPane().add(XLab, gridBagConstraints);

        LocLab.setText("Initial Location");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 7;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        getContentPane().add(LocLab, gridBagConstraints);

        AddField.setText("Add as New Term");
        AddField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AddFieldActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 8;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        getContentPane().add(AddField, gridBagConstraints);

        SaveMod.setText("Modify Term");
        SaveMod.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SaveModActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 9;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        getContentPane().add(SaveMod, gridBagConstraints);

        RemoveField.setText("Remove Term");
        RemoveField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                RemoveFieldActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 8;
        getContentPane().add(RemoveField, gridBagConstraints);

        TermName.setColumns(12);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 7;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        getContentPane().add(TermName, gridBagConstraints);

        TermWeight.setColumns(5);
        TermWeight.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                TermWeightActionPerformed(evt);
            }
        });
        TermWeight.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                TermWeightKeyTyped(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 7;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        getContentPane().add(TermWeight, gridBagConstraints);

        Desc.setText("<html>This is a candidate for a POI.  You can simply skip it and get the next candidate, or<br>Name it,  modify the terms, weigths color, and location and make it a POI and get the next candidate</html>");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 5;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        getContentPane().add(Desc, gridBagConstraints);
        Desc.getAccessibleContext().setAccessibleName("<html>This is a candidate for a POI.  You can skip it, or modify the terms, weigths color, and location<bR> and save it as a new POI</html>");

        SavePOI.setText("Make POI & Next");
        SavePOI.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SavePOIActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 11;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        getContentPane().add(SavePOI, gridBagConstraints);

        NextPOI.setText("Skip as POI");
        NextPOI.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                NextPOIActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 11;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        getContentPane().add(NextPOI, gridBagConstraints);

        DocsLab.setText("Documents included in this cluster");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        getContentPane().add(DocsLab, gridBagConstraints);

        TermsLab.setText("Suggested terms for this cluster");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        getContentPane().add(TermsLab, gridBagConstraints);

        Docs.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        DocSP.setViewportView(Docs);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        getContentPane().add(DocSP, gridBagConstraints);

        ClusterLab.setText("Cluster Info");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        getContentPane().add(ClusterLab, gridBagConstraints);

        POILab.setText("POI Info");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        getContentPane().add(POILab, gridBagConstraints);

        AbortBut.setText("Abort");
        AbortBut.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AbortButActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 11;
        getContentPane().add(AbortBut, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents

    private void TermsMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_TermsMouseClicked
//if (evt.getModifiers()==java.awt.event.InputEvent.BUTTON3_MASK)
        int index= Terms.locationToIndex(new java.awt.Point(evt.getX(),evt.getY()));
        Terms.setSelectedIndex(index);
        String tmp = (String)Terms.getSelectedValue();
        String[] parts = tmp.split(",");
        TermName.setText(parts[0]);
        TermWeight.setText(parts[1]);
    }//GEN-LAST:event_TermsMouseClicked

    private void SaveModActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SaveModActionPerformed
        // TODO add your handling code here:
        int index = Terms.getSelectedIndex();
        DefaultListModel dlm = (DefaultListModel)Terms.getModel();
        dlm.setElementAt(TermName.getText()+","+TermWeight.getText(),index);
    }//GEN-LAST:event_SaveModActionPerformed

    private void RemoveFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_RemoveFieldActionPerformed
        // TODO add your handling code here:
        int index = Terms.getSelectedIndex();
        DefaultListModel dlm = (DefaultListModel)Terms.getModel();
        dlm.removeElementAt(index);
        TermName.setText("");
        TermWeight.setText("");
        if (index<Terms.getLastVisibleIndex())
            Terms.setSelectedIndex(index);
        else
            Terms.setSelectedIndex(Terms.getLastVisibleIndex());
    }//GEN-LAST:event_RemoveFieldActionPerformed

    private void AddFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AddFieldActionPerformed
        // TODO add your handling code here:
        DefaultListModel dlm = (DefaultListModel)Terms.getModel();
        dlm.addElement(TermName.getText()+","+TermWeight.getText());
        Terms.setSelectedIndex(Terms.getLastVisibleIndex());
    }//GEN-LAST:event_AddFieldActionPerformed

    private void ColorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ColorActionPerformed
        // TODO add your handling code here:
        java.awt.Color existingbg = this.getBackground();
        java.awt.Color newbg = JColorChooser.showDialog(null, "JColorChooser Sample", existingbg);
        if (newbg != null) {
          this.setBackground(newbg);
          this.getContentPane().setBackground(newbg);
        }
    }//GEN-LAST:event_ColorActionPerformed

    private void NameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_NameActionPerformed
        // TODO add your handling code here:
        this.name = Name.getText();
    }//GEN-LAST:event_NameActionPerformed

    private void XLocActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_XLocActionPerformed
        // TODO add your handling code here:
        this.X = new Double(XLoc.getText()).doubleValue();
    }//GEN-LAST:event_XLocActionPerformed

    private void YLocActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_YLocActionPerformed
        // TODO add your handling code here:
        this.Y = new Double(YLoc.getText()).doubleValue();
    }//GEN-LAST:event_YLocActionPerformed

    private void NameFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_NameFocusLost
        // TODO add your handling code here:
        this.name = Name.getText();
    }//GEN-LAST:event_NameFocusLost

    private void XLocFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_XLocFocusLost
        // TODO add your handling code here:
        this.X = new Double(XLoc.getText()).doubleValue();
    }//GEN-LAST:event_XLocFocusLost

    private void YLocFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_YLocFocusLost
        // TODO add your handling code here:
        this.Y = new Double(YLoc.getText()).doubleValue();
    }//GEN-LAST:event_YLocFocusLost

    private void SavePOIActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SavePOIActionPerformed
        // TODO add your handling code here:
        this.setVisible(false);
        this.makePOI=true;
    }//GEN-LAST:event_SavePOIActionPerformed

    private void NextPOIActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_NextPOIActionPerformed
        // TODO add your handling code here:
         this.setVisible(false);
        this.makePOI=false;
    }//GEN-LAST:event_NextPOIActionPerformed

    private void TermWeightKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_TermWeightKeyTyped
        // TODO add your handling code here:
        int index = Terms.getSelectedIndex();
        DefaultListModel dlm = (DefaultListModel)Terms.getModel();
        dlm.setElementAt(TermName.getText()+","+TermWeight.getText(),index);
    }//GEN-LAST:event_TermWeightKeyTyped

    private void AbortButActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AbortButActionPerformed
        // TODO add your handling code here:
        this.setVisible(false);
        this.Abort=true;
}//GEN-LAST:event_AbortButActionPerformed

    private void TermWeightActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_TermWeightActionPerformed
        // TODO add your handling code here:
         int index = Terms.getSelectedIndex();
        DefaultListModel dlm = (DefaultListModel)Terms.getModel();
        dlm.setElementAt(TermName.getText()+","+TermWeight.getText(),index);
    }//GEN-LAST:event_TermWeightActionPerformed

   public void setColor(int r, int g, int b){
        this.bgcolor = new java.awt.Color(r,g,b);
        this.setBackground(bgcolor);
    }
   public void setpos(){
        java.awt.Rectangle rectOwner, rectDialog;
        rectOwner = PParent.getBounds();
        rectDialog = this.getBounds();
        setLocation(rectOwner.x + rectOwner.width /2 - rectDialog.width /2,
        rectOwner.y + rectOwner.height/2 - rectDialog.height /2);
   }
   public java.awt.Color getColor(){
        return this.getBackground();
    }
   public void setXloc(double x){
        this.X = x;
        XLoc.setText(new Double(X).toString());
    }
   public double getXloc(){
        return X;
    }
   public void setYloc(double y){
        this.Y = y;
        YLoc.setText(new Double(Y).toString());
    }
   public double getYloc(){
        return Y;
    }
   public void setName(String name){
        this.name = name;
        Name.setText(name);
    }
   public void setPLabel(String txt){

        ClusterLab.setText(txt);
    }
   public void setCLabel(String txt){

        POILab.setText(txt);
    }
   public String getName(){
        return name;
    }
    public Boolean MakePOI(){
        return makePOI;
    }
    public Boolean AbortProcess(){
        return Abort;
    }
   public void setTerm(String term){
        this.dtl.addElement(term);
    }
   public String getTerm(){
       if (this.dtl.isEmpty()){
        return null;
       }
       else{
        String tmp = (String) this.dtl.firstElement();
        this.dtl.remove(0);
        return tmp;

       }
    }
    public void setDoc(String term){
        this.ddl.addElement(term);
    }
   public String getDoc(){
       if (this.ddl.isEmpty()){
        return null;
       }
       else{
        String tmp = (String) this.ddl.firstElement();
        this.ddl.remove(0);
        return tmp;

       }
    }

   private javax.swing.DefaultListModel ddl=new javax.swing.DefaultListModel() ;
   private javax.swing.DefaultListModel dtl=new javax.swing.DefaultListModel() ;
   private String name=null;
   private java.awt.Color bgcolor=new java.awt.Color(255,255,255);
   private double  X=0.0, Y=0.0;
   private boolean makePOI=false;
   private boolean Abort=false;
   private spring.PoiEditor PParent;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton AbortBut;
    private javax.swing.JButton AddField;
    private javax.swing.JLabel ClusterLab;
    private javax.swing.JButton Color;
    private javax.swing.JLabel Desc;
    private javax.swing.JScrollPane DocSP;
    private javax.swing.JList Docs;
    private javax.swing.JLabel DocsLab;
    private javax.swing.JLabel LocLab;
    private javax.swing.JTextField Name;
    private javax.swing.JLabel NameLab;
    private javax.swing.JButton NextPOI;
    private javax.swing.JLabel POILab;
    private javax.swing.JButton RemoveField;
    private javax.swing.JButton SaveMod;
    private javax.swing.JButton SavePOI;
    private javax.swing.JTextField TermName;
    private javax.swing.JScrollPane TermSP;
    private javax.swing.JTextField TermWeight;
    private javax.swing.JList Terms;
    private javax.swing.JLabel TermsLab;
    private javax.swing.JLabel XLab;
    private javax.swing.JTextField XLoc;
    private javax.swing.JTextField YLoc;
    private javax.swing.JLabel Ylab;
    // End of variables declaration//GEN-END:variables

}
